//
//  PostDetailImgsView.m
//  sensen_ios
//
//  Created by pro on 16/3/2.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "PostDetailImgsView.h"
#import "BigImageViewController.h"
#import "ZoomImageView.h"
#define KMarginWidth 8
#define KImgHeight 160


@implementation PostDetailImgsView

- (void)setImgHeightAry:(NSArray *)imgHeightAry {
    _imgHeightAry = imgHeightAry;
}

- (void)setImgAry:(NSArray *)imgAry {
    if (self.subviews) {
        for (UIView *view in self.subviews) {
            [view removeFromSuperview];
        }
    }
    _imgAry = imgAry;
    
    if (_imgAry.count == 0) {
        self.bounds = CGRectZero;
    }
    else {
        // 创建图片视图
        CGFloat x, y = 0, width, height;
        width = SCREEN_WIDTH - 30;
        for (int i = 0; i < _imgAry.count; i ++) {
            x = 0;
            height = [_imgHeightAry[i] floatValue];
            if (i == 0) {
                y = 0;
            } else {
                y += ([_imgHeightAry[i - 1] floatValue] + KMarginWidth);
            }
            UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
            imgView.tag = i + 4000;
            imgView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_pushToBigImgViewVC:)];
            [imgView addGestureRecognizer:tap];
            [self addSubview:imgView];

            // 加载网络图片
            [imgView setImageURLStr:_imgAry[i] placeholder:[UIImage imageWithColor:[UIColor colorWithWhite:0.8 alpha:1]]];
        }
    }
    
}

- (void)_pushToBigImgViewVC:(UITapGestureRecognizer *)tap {
    
    BigImageViewController *vc = [[BigImageViewController alloc] init];
    vc.imageData = self.imgAry;
    NSInteger index = tap.view.tag - 4000;
    vc.indexPath = [NSIndexPath indexPathForItem:index inSection:0];
    
    [self.viewController presentViewController:vc animated:YES completion:nil];
}

@end
